package cn.gailvlun.gll.presentation.charge.vm

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.config.failed
import cn.gailvlun.gll.config.loading
import cn.gailvlun.gll.config.success
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.ListRes
import cn.gailvlun.gll.net.pay.PayOrderModel
import cn.gailvlun.gll.presentation.charge.model.Outcome
import cn.gailvlun.gll.util.RxUtil

class ChargeListViewModel : ViewModel {
    var orderOutcome = MutableLiveData<Outcome<ListRes<PayOrderModel>>>()
    constructor() : super()

    fun getOrderList(direction: String?, lastId: Int?) {
        orderOutcome.loading(true)
        HttpMethods.getPayService()
                .getOrderList(direction, lastId, null)
                .compose(RxUtil.applyScheduler())
                .subscribe(object: HttpSubscriber<ListRes<PayOrderModel>>() {
                    override fun onSuccess(response: ListRes<PayOrderModel>) {
                        response.first = direction == null
                        orderOutcome.success(response)
                    }

                    override fun onFailure(errMsg: String, response: ListRes<PayOrderModel>?, code: Int) {
                        orderOutcome.failed(errMsg)
                    }
                })
    }


}